/**
 * 
 */
package com.shengruan.index.action;

import com.shengruan.admin.jdbc.ArticleJdbc;
import com.shengruan.admin.jdbc.ContentJdbc;
import com.shengruan.admin.vo.Article;
import com.shengruan.admin.vo.Content;
import com.shengruan.common.BaseAction;
import com.shengruan.framework.pager.Pager;
import org.apache.commons.lang.math.NumberUtils;

import java.util.List;

public class ArticleAction extends BaseAction {

	private ArticleJdbc articleJdbc = (ArticleJdbc)this.getJdbc(ArticleJdbc.class);
	private ContentJdbc contentJdbc = (ContentJdbc)this.getJdbc(ContentJdbc.class);
    
    public void loadEvents() throws Exception {
        int currentPage = 1;
        try{currentPage = Integer.parseInt(input("currentPage"));}catch(Exception ex){}
        Pager pager = new Pager(currentPage,articleJdbc.getRows(1),4);
        List<Article> articleList = articleJdbc.findWithPage(currentPage, 4,1);
        this.setRequest("pager", pager);
        this.setRequest("articleList", articleList);
        this.setRequest("articleTopList", this.articleJdbc.getArticleByTop(1, 8));

        this.forward("events.html");
    }
    
    public void loadNews() throws  Exception {
        int currentPage = 1;
        try{currentPage = Integer.parseInt(input("currentPage"));}catch(Exception ex){}
        Pager pager = new Pager(currentPage,articleJdbc.getRows(2),2);
        List<Article> articleList = articleJdbc.findWithPage(currentPage, 2,2);
        this.setRequest("pager", pager);
        this.setRequest("articleList", articleList);
        this.setRequest("articleTopList", this.articleJdbc.getArticleByTop(2, 8));

        this.forward("news.html");
    }
    
    public void loadMedia() throws Exception {
        int currentPage = 1;
        try{currentPage = Integer.parseInt(input("currentPage"));}catch(Exception ex){}
        Pager pager = new Pager(currentPage,articleJdbc.getRows(3),2);
        List<Article> articleList = articleJdbc.findWithPage(currentPage, 2,3);
        this.setRequest("pager", pager);
        this.setRequest("articleList", articleList);
        this.setRequest("articleTopList", this.articleJdbc.getArticleByTop(3, 8));

        this.forward("media.html");
    }
    
    public void articleDetails() throws Exception {
        String id = this.input("id");
        if(org.apache.commons.lang.StringUtils.isNotEmpty(id)) {
            Article article = articleJdbc.getById(NumberUtils.toInt(id));
            this.setRequest("article", article);
            this.forward("article-details.html");
        }else {
            this.loadNews();
        }
    }
    
	/***
	 * 获得详细信息
	 * @throws Exception
	 */
	public void show() throws Exception
	{
		int id = 1;
		try
		{
			id = Integer.parseInt(request.getParameter("id"));
		}
		catch(Exception ex)
		{
			
		}
		Content content = contentJdbc.getById(id);
		if(content==null)
		{
			this.redirect("/default.jsp");
			return;
		}
		request.setAttribute("content",content);
		this.forward("/showArc.jsp");
	}
	
	/***
	 * 显示公告详细信息
	 */
	public void showAff() throws Exception
	{
		int id = 1;
		try
		{
			id = Integer.parseInt(request.getParameter("id"));
		}
		catch(Exception ex)
		{
			
		}
		
		Article article = articleJdbc.getById(id);
		if(article==null)
		{
			this.redirect("/default.jsp");
			return;
		}
	
		request.setAttribute("article",article);
		this.forward("/showAff.jsp");
	}
}
